#!/bin/bash

function check_inline()
{
    echo now path=`pwd`
    if [ "$1" == "SendFullProjectForApple" ];then
        git clone ssh://gerrit@192.168.8.250:29418/ios/SendFullProjectForApple
    else
        git clone ssh://gerrit@192.168.8.250:29418/android/standard/$1
    fi

    cd $1
    if [ "$1" == "AndroidReceiver-APP" ];then
        git checkout master
    elif [ "$1" == "AndroidSender-APP" ];then
        git checkout newmaster
    else
        git checkout develop
    fi

    git log  --decorate --pretty=oneline > mytest
    #排除无用的分支
    if [ "$1" == "AndroidReceiver-SDK" ];then
        git branch -a |grep "origin/r"|grep -v "release"|grep -v "r1.3"|grep -v "r5.4.0"|grep -v "r5.4.2_pptv"|grep -v "r5.4.1_bugfix" | \
        grep -v "r5.4.2_bugfix" |grep -v "r5.5.10_skyworthbox_1.0" |grep -v "r5.6.0_pptv_1.0" |grep -v "r5.5.16_mi_1.0" | grep -v "r5.5.18_mi_1.0" \
        | grep -v "r5.6.1_feixun_1.0" > test3
    fi

    if [ "$1" == "AndroidReceiver-APP" ];then
        git branch -a |grep "origin/r" |grep -v "origin/r6" |grep -v "r7.0.0_feature$" |grep -v "r7.0.0_feature_1.1$" |grep -v "tmp$"> test3
    fi

    if [ "$1" == "AndroidSender-SDK" ];then
        git branch -a |grep "origin/r"|grep -v "r2.5"|grep -v "r2.6.1_bugfix" |grep -v "r2.6.1_huawei_1.0" > test3
    fi

    if [ "$1" == "AndroidSender-APP" ];then
        git branch -a |grep "origin/r"|grep -v "release"|grep -v "r2.8.1" |grep -v "r2.9.0_hudong_1.0"  > test3
    fi

    if [ "$1" == "SendFullProjectForApple" ];then
        git branch -a |grep "origin/r" | grep -v "release" |grep -v "tmp$" > test3
    fi

    cat mytest |grep "origin/r" |grep -v  "origin/release" |awk -F "(" '{print $2}' |awk -F ")" '{print $1}' > test1
    new_flag_tmp=`grep -rn "HEAD" test1`
    if [ "$1" == "SendFullProjectForApple" ];then
        new_flag=`echo $new_flag_tmp |grep "tag_app_r" `
    else
        new_flag=`echo $new_flag_tmp |grep "tag_r" `
    fi

    if [ "x$new_flag" != "x" ];then
        echo new_flag=$new_flag
        sed /'HEAD'/d test1 >test1_tmp
        mv test1_tmp test1
    fi
    #将逗号换成换行
    sed -i 's/,/\n/g' test1
    #删除包含tag:的行以及行头的空格以及origin/字段
    if [ "$1" == "AndroidReceiver-APP" ];then
        sed /'tag:'/d test1 | sed 's/ //g' | sed 's/origin\///g' |sed /'master'/d |sed /'HEAD'/d >test2
    else
        sed /'tag:'/d test1 | sed 's/ //g' | sed 's/origin\///g' >test2
    fi
    sum_num=`cat test2|wc -l`
    echo sum_num=$sum_num
    for ((i=1;i<=$sum_num;i++))
        do
        {
            match_branch=`sed -n "$i"p test2`
            branch_flag=`grep $match_branch test3`
            if [ "x$branch_flag" != "x" ];then
                echo  $match_branch already in develop
                sed /$match_branch/d test3 > test3_tmp
                mv test3_tmp test3
            fi
        }
    done

    echo now outside develop branch as follow:
    #删除行头空格和前缀
    sed 's/ //g' test3 >tmp_test3
    sed 's/remotes\/origin\///g' tmp_test3 >test3
    if [ "$1" == "AndroidReceiver-SDK" ];then
        echo "r5.4.0_feature_huashu" >> test3
        echo "r5.4.0_kumiao_bugfix" >> test3
        echo "r5.4.0_damai_1.1" >> test3
        echo "r5.4.0_douyu_1.0" >> test3
        echo "develop" >> test3
        echo "leboapk_develop" >> test3
    fi

    if [ "$1" == "AndroidReceiver-APP" ];then
        echo "r6.8.2_pptv_2.0" >> test3
        echo "master" >> test3
    fi

    if [ "$1" == "AndroidSender-SDK" ];then
        echo "develop" >> test3
    fi

    if [ "$1" == "AndroidSender-APP" ];then
        echo "newmaster" >> test3
    fi

    if [ "$1" == "SendFullProjectForApple" ];then
        echo "develop" >> test3
    fi

    cat test3
}

function modify_config()
{
    cp /root/.jenkins/jobs/$1/config.xml  "$2"_config.xml
    fit_line_tmp=`grep -rn "<string>r" "$2"_config.xml |sed -n 1p |awk -F ":" '{print $1}'`
    out_line=`cat test3 |wc -l`
    fit_line=`expr $fit_line_tmp - 1`
    echo fit line=$fit_line and out_line=$out_line
    if [ "$2" == "rec_sdk"  -o  "$2" == "send_sdk" -o "$2" == "ios"  ];then
        sed /'<string>develop'/d "$2"_config.xml > tmp_"$2"_config.xml
    fi

    if [ "$2" == "rec_app" ];then
        sed /'<string>master'/d "$2"_config.xml > tmp_"$2"_config.xml
    fi

    if [ "$2" == "send_app" ];then
        sed /'<string>newmaster'/d "$2"_config.xml > tmp_"$2"_config.xml
    fi

    if [ "$2" == "rec_sdk" ];then
        sed /'<string>leboapk_develop'/d tmp_"$2"_config.xml > tmp_tmp_"$2"_config.xml
        mv tmp_tmp_"$2"_config.xml tmp_"$2"_config.xml
    fi

    mv tmp_"$2"_config.xml "$2"_config.xml
    sed /'<string>r'/d "$2"_config.xml > tmp_"$2"_config.xml
    mv tmp_"$2"_config.xml "$2"_config.xml

    for ((j=1;j<=$out_line;j++))
        do
        {
            line_str=`sed -n "$j"p test3`
            line_string="<string>$line_str</string>"
            echo after insert $fit_line $line_string
            sed -i "$fit_line"'a\'"$line_string" "$2"_config.xml
            fit_line=`expr $fit_line + 1`
        }
        done
     mv "$2"_config.xml /root/.jenkins/jobs/$1/config.xml
     curl -u admin:wangzp@360 -X POST http://192.168.8.246:8080/jenkins/view/Test_Android/job/$1/reload
}


scp /usr/local/scripts/build_scripts/phoenix_ios.sh  jenkins@192.168.8.210:/Users/jenkins/lebo/
scp -r  /usr/local/scripts/build_scripts/* root@192.168.8.233:/usr/local/scripts/build_scripts/
scp -r  /usr/local/scripts/build_scripts/* root@192.168.8.204:/usr/local/scripts/build_scripts/

exit 0
scp /usr/local/scripts/build_scripts/push_tag.sh  jenkins@192.168.8.210:/Users/jenkins/lebo/
scp /usr/local/scripts/build_scripts/util.sh   jenkins@192.168.8.210:/Users/jenkins/lebo/
scp /usr/local/scripts/build_scripts/yun_ios.sh   jenkins@192.168.8.210:/Users/jenkins/lebo/
scp /usr/local/scripts/build_scripts/lebo_phoenix.sh   jenkins@192.168.8.210:/Users/jenkins/lebo/
ssh jenkins@192.168.8.210 "sudo mv /Users/jenkins/lebo/util.sh  /usr/local/scripts/build_scripts/util.sh; sudo chmod 777 /Users/jenkins/lebo/*"

cur_path=`pwd`
echo cur_path=$cur_path
echo will modify Android Receiver SDK
check_inline AndroidReceiver-SDK
modify_config Android-Reciever-Sdk rec_sdk

echo will modify Android Receiver APP
cd $cur_path
check_inline AndroidReceiver-APP
modify_config Android-Reciever-App rec_app

echo will modify Android Sender SDK
cd $cur_path
check_inline AndroidSender-SDK
modify_config Android-Sender-Sdk send_sdk

echo will modify Android Sender APP
cd $cur_path
check_inline AndroidSender-APP
modify_config Android-Sender-App send_app

echo will modify ios
cd $cur_path
check_inline SendFullProjectForApple
modify_config Ios_send_module ios

echo will do clean $cur_path dir
cd $cur_path
rm Android* -rf


